import Vue from 'vue'
import VueRouter from 'vue-router'
import LoginView from "@/views/LoginView.vue";
import AdminView from "@/views/AdminView.vue";
import store from "@/store";
import {Message} from "element-ui";
import WelcomeView from "@/views/WelcomeView.vue";
import UserView from "@/views/UserView.vue";
import RoleView from "@/views/RoleView.vue";

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'login',
    component: LoginView
  },
  {
    path: '/admin',
    name: 'admin',
    component: AdminView,
    children: [
      {
        path: '',
        name: 'welcome',
        component: WelcomeView
      },
      {
        path: 'user',
        name: 'user',
        component: UserView
      },
      {
        path: 'role',
        name: 'role',
        component: RoleView
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.name !== 'login'){
    if (store.state.username === ''){
      Message.error("未登录")
      next({name : "login"})
    }else {
      next()
    }
  }else {
    next()
  }
})

export default router
